package edu.yuhf.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.yuhf.dao.NativePlaceJdbcDaoImplTest;

import edu.yuhf.dao.NativePlaceJdbcDaoImpl;
import edu.yuhf.dao.iface.NativePlaceDao;
import edu.yuhf.domain.NativePlace;

@WebServlet("/nativePlace.servlet")
public class NativePlaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static Logger log=Logger.getLogger(NativePlaceJdbcDaoImplTest.class);
	
	private NativePlaceDao nativePlaceDao=new NativePlaceJdbcDaoImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String param=null==request.getParameter("param")?"":request.getParameter("param");
		String code=null==request.getParameter("code")?"":request.getParameter("code");
		
		PrintWriter out=response.getWriter();
		Map<String,Object> map=new HashMap<String,Object>();
		if("init".equals(param)) {
			List<NativePlace> list=nativePlaceDao.getProvinces();
			map.put("result","success");
			map.put("list", list);
		}
		if("getCity".equals(param)) {
			List<NativePlace> list=nativePlaceDao.getCitys(code);
			map.put("result","success");
			map.put("list", list);
		}
		String jsonString=JSONObject.toJSONString(map);
		out.print(jsonString);
		out.flush();
		out.close();		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
